<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  
</body>
<script>
  var arr = [100,1, 10, 21, 2, 8,3,5,1]
  // console.log(arr.sort(function (a, b) { // 方法排序
  //   return a > b
  // }))
  // function sort (arr) { // 插入排序——从小到大
  //   for (let i = 0; i < arr.length; i++) {
  //     for (let j = i -1; j >= 0; j--) {
  //       if (arr[j] > arr[j+1]) {
  //         let value = arr[j]
  //         arr[j] = arr[j+1]
  //         arr[j+1] = value
  //       }
  //     }
  //   }
  //   return arr
  // }

  function sort (arr) { // 冒泡排序——从小到大
    for (let i = 0; i < arr.length; i++) {
      for (let j = 0; j < arr.length - 1; j++) {
        if (arr[j] > arr[j+1]) {
          // [arr[j+1], arr[j]] = [arr[j], arr[j+1]]
          let value = arr[j]
          arr[j] = arr[j+1]
          arr[j+1] = value
        }       
      }
    }
    return arr
  }
  let result = sort(arr)
  console.log(result)
</script>
</html>